import Vue from 'vue'

import VueRouter from 'vue-router'
const register = () => import('@/views/register.vue')
const Login = () => import('@/views/Login.vue')
const userInfo = () => import('@/views/userInfo.vue')
const edit = () => import('@/views/edit.vue')
const Home =  () => import ('@/views/Home.vue')
const Article = () => import ('@/views/Article.vue')
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    component:Home
  },
  {
    path:'/register',
    component:register
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/userinfo',
    component:userInfo,
    meta : {
      istoken : true
    }
  },
  {
    path:'/edit',
    component:edit,
    meta : {
      istoken:true
    }
  },
  {
    path:'/article/:id',
    component:Article
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

router.beforeEach((to,from,next) => {
  if( !localStorage.getItem('token')&& !localStorage.getItem('id')&&to.meta.istoken === true ) {
    router.push('/login')
    Vue.prototype.$msg.fail('请重新登录')
    return
  }
  next()
})
export default router
